<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cookie;
use think\Request;
use app\index\model\Order;
use app\index\model\KnowUser;
use app\index\service\UserService;




class Profile extends Common
{
	public function __construct(Request $request)
	{
		parent::__construct();
		$param = $request->param();
		$user = user();
		$admin = unserialize(cookie('admin'));
		$adminStatus = 0;
		if ($param['userid'] && $param['preview'] && $admin)
		{
			$userData = KnowUser::getUserOneIds($param['userid']);
			$userData['nickname'] = $userData['know_nickname'] ?: checkNickname($userData['know_phone']);
			$adminStatus = 1;
			$user = ['id' => $param['userid'], 'phone' => $userData['know_phone']];
			$this->assign('userData', $userData);
		}
		if (!$user)
			$this->redirect('/login.html?redirect=http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
		$this->user = $user;
		$this->assign('user', $user);
		$this->assign('param', $param);
		$this->assign('adminStatus', $adminStatus);
	}

	public function course(Request $request)
	{
		$data = UserService::getUserNav(['user' => $this->user]);
		return $this->fetch('', [
			'type' => 'course',
			'data' => $data,
			'title' => '课程-个人中心'
		]);
	}

	public function progress(Request $request)
	{
		return $this->fetch('', [
			'type' => 'progress',
			'title' => '进度-个人中心'
		]);
	}

	public function mine(Request $request)
	{
		return $this->fetch('', [
			'type' => 'mine',
			'title' => '个人资料-个人中心'
		]);
	}


	public function order(Request $request)
	{
		list($data, $navData) = Order::getUserData(['user' => $this->user]);
		return $this->fetch('', [
			'type' => 'order',
			'data' => $data,
			'navData' => $navData,
			'title' => '订单-个人中心'
		]);
	}

	public function logs(Request $request)
	{
		$param = $request->param();
		$param['user'] = $this->user;
		$data = UserService::getUserProgress($param);
		return json([
			'data' => $data['data'],
			'count' => $data['total'],
			'nums' => $data['nums'],
		]);
	}

	public function update(Request $request)
	{
		$user = user();
		$param = $request->param();
		KnowUser::where('know_user_id', $user['id'])->update([
			'know_avatar' => $param['avatar'],
			'know_nickname' => $param['nickname'],
			'know_desc' => $param['desc'],
		]);
		return json(['msg' => '修改成功']);
	}
}